package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.JuBaoActivity;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.PublishCommentActivity;
import com.xgkj.diyiketang.data.CommentInfo;
import com.xgkj.diyiketang.util.ImageCache;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/21.
 */
public class CommentAdapter extends BaseAdapter {


    private Context mContext;
    private String mVidoeId ;
    private int mType;

    private List<CommentInfo> mList = new ArrayList<CommentInfo>();

    /**
    * type 1 视频评论
     *     2 照片评论
     *     3 帖子评论
    * */
    public CommentAdapter(Context context,String videoid,int type) {
        mContext = context;
        mVidoeId = videoid;
        mType = type;
    }


    public void updateData(List<CommentInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.comment_item,null);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_icon);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.commenter_name);
            childHolder.time = (TextView) convertView.findViewById(R.id.comment_time);
            childHolder.commentText = (TextView) convertView.findViewById(R.id.comment_neirong);
            childHolder.sendMessageBtn = (Button) convertView.findViewById(R.id.fabiaopinglun);
            childHolder.jubaoBtn = (Button) convertView.findViewById(R.id.jubao);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        final CommentInfo  commentInfo = mList.get(position);
        if(commentInfo != null){
            String headImgUrl = commentInfo.getUserLogo();
            childHolder.headImg.setTag(headImgUrl);
            if(headImgUrl != null && headImgUrl.length()>0){
//                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg,headImgUrl,new ViewUtil.Size(ViewUtil.dip2px(mContext,35),ViewUtil.dip2px(mContext,35)));
                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext,35),ViewUtil.dip2px(mContext,35));
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(headImgUrl,size));
                if ( cacheBitmap != null){
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                }else {
                    childHolder.headImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg,headImgUrl,size);
                }

            }
            if(commentInfo.getNickName()!=null){
                try {
                    childHolder.nickNameText.setText(URLDecoder.decode(commentInfo.getNickName(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            childHolder.time.setText(commentInfo.getTime());
            try {
                childHolder.commentText.setText(URLDecoder.decode(commentInfo.getCommentContent(),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


            childHolder.sendMessageBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PublishCommentActivity.class);
                    intent.putExtra("UserId",commentInfo.getUserId());
                    intent.putExtra("VideoId",mVidoeId);
                    intent.putExtra("NickName",commentInfo.getNickName());
                    intent.putExtra("type",mType);
                    mContext.startActivity(intent);
                }
            });

            childHolder.jubaoBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent jbIntent = new Intent(mContext, JuBaoActivity.class);
                    mContext.startActivity(jbIntent);
                }
            });
        }

        return convertView;
    }

    class ChildHolder{
        Button jubaoBtn;
        ImageView headImg;
        TextView nickNameText;
        TextView time;
        TextView commentText;
        Button sendMessageBtn;
    }
}
